package com.culturalCenter.dataCenter.service.impl.training;

import com.culturalCenter.dataCenter.customEntity.responseParam.training.TrainingPopularAnalysisResponse;
import com.culturalCenter.dataCenter.customEntity.training.CustomCommentList;
import com.culturalCenter.dataCenter.customEntity.training.CustomInteractive;
import com.culturalCenter.dataCenter.customEntity.training.CustomTrainingComment;
import com.culturalCenter.dataCenter.customEntity.training.CustomTrainingsNumber;
import com.culturalCenter.dataCenter.mapper.training.TrainingCommentCountMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingCommentMapper;
import com.culturalCenter.dataCenter.mapper.training.TrainingInfoCountMapper;
import com.culturalCenter.dataCenter.service.training.TrainingPopularAnalysisService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Optional;


/**
 * @author : weicai
 * @date : 18:13 2020/10/12
 */
@Service
public class TrainingPopularAnalysisServiceImpl implements TrainingPopularAnalysisService {

    @Resource
    private TrainingInfoCountMapper trainingInfoCountMapper;
    @Resource
    private TrainingCommentCountMapper trainingCommentCountMapper;
    @Resource
    private TrainingCommentMapper trainingCommentMapper;

    @Override
    public TrainingPopularAnalysisResponse getPopularAnalysisData(String trainingId, String branchId) {
        TrainingPopularAnalysisResponse trainingPopularAnalysisResponse = new TrainingPopularAnalysisResponse();
        //培训人数情况
        List<CustomTrainingsNumber> trainingsNumberList = trainingInfoCountMapper.queryTrainingsByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(trainingsNumberList)) {
            trainingsNumberList.forEach(val ->{
                double v = Double.parseDouble(Optional.ofNullable(val.getExceedRate()).orElse("0"));
                double num = (1-Math.exp(-v))*100;
                DecimalFormat decimalFormat = new DecimalFormat("#.##");
                val.setExceedRate(decimalFormat.format(num));
            });
            trainingPopularAnalysisResponse.setTrainingsNumberList(trainingsNumberList);
        }

        // 高于多少培训
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        List<CustomTrainingComment> rankList = trainingCommentCountMapper.selectRank(branchId, trainingId);
        if (CollectionUtils.isNotEmpty(rankList)) {
            int sum = trainingCommentCountMapper.countAll(branchId);
            // 培训排名
            int rank = rankList.stream().filter(r -> r.getTrainingId().equals(trainingId)).mapToInt(CustomTrainingComment::getRank).sum();
            // 高于多少培训（百分比）
            String rate = decimalFormat.format((1 - (float) rank / sum) * 100);
            for (CustomTrainingComment customTrainingComment : rankList) {
                customTrainingComment.setSum(sum);
                customTrainingComment.setHigh(sum == 1 ? "100" : rate);
            }
            trainingPopularAnalysisResponse.setRankList(rankList);
        }

        // 高于同分类多少培训
        List<CustomTrainingComment> categoryRankList = trainingCommentCountMapper.selectCategoryRankList(branchId, trainingId);
        if (CollectionUtils.isNotEmpty(categoryRankList)) {
            int sum = trainingCommentCountMapper.countCategory(branchId, trainingId);
            // 同分类该培训排名
            int rank = categoryRankList.stream().filter(r -> r.getTrainingId().equals(trainingId)).mapToInt(CustomTrainingComment::getRank).sum();
            // 高于同分类培训（百分比）
            String rate = decimalFormat.format((1 - (float) rank / sum) * 100);
            for (CustomTrainingComment customTrainingComment : categoryRankList) {
                customTrainingComment.setSum(sum);
                if (sum == 1) {
                    customTrainingComment.setHigherThenCategory("100");
                } else {
                    customTrainingComment.setHigherThenCategory(rate);
                }
            }
            trainingPopularAnalysisResponse.setCategoryRankList(categoryRankList);
        }

        // 评星和点赞统计
        List<CustomInteractive> commentInfo = trainingCommentCountMapper.queryGradeByTrainingId(trainingId);
        if (CollectionUtils.isNotEmpty(commentInfo)) {
            int sum = commentInfo.stream().mapToInt(CustomInteractive::getCommentNum).sum();
            for (CustomInteractive customInteractive : commentInfo) {
                customInteractive.setTotalCommentNum(sum);
            }
            trainingPopularAnalysisResponse.setCommentInfo(commentInfo);
        }

        return trainingPopularAnalysisResponse;
    }

    @Override
    public List<CustomCommentList> getCommentList(String trainingId) {
        return trainingCommentMapper.queryByTrainingId(trainingId);
    }

}
